<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book python基础语言 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>生成器 | python基础语言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../02day/section.3.html" />
    
    
    <link rel="prev" href="../01day/section.1.3.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="1.4"
        data-chapter-title="生成器"
        data-filepath="02day/section.4.md"
        data-basepath=".."
        data-revision="Fri Mar 17 2017 08:48:09 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="01day/index.html">
            
                
                    <a href="../01day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象进阶
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="01day/section.1.0.html">
            
                
                    <a href="../01day/section.1.0.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        元类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01day/section.1.1.html">
            
                
                    <a href="../01day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        python是动态语言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="01day/section.1.3.html">
            
                
                    <a href="../01day/section.1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        __slots__
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="1.4" data-path="02day/section.4.html">
            
                
                    <a href="../02day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        生成器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="02day/section.3.html">
            
                
                    <a href="../02day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        迭代器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="02day/section.1.html">
            
                
                    <a href="../02day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        闭包
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="02day/section.2.html">
            
                
                    <a href="../02day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        装饰器
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="02day/index.html">
            
                
                    <a href="../02day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="03day/section.0.2.html">
            
                
                    <a href="../03day/section.0.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        import导入模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="03day/section.1.1.html">
            
                
                    <a href="../03day/section.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        循环导入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="03day/section.0.3.html">
            
                
                    <a href="../03day/section.0.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        作用域
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="03day/section.2.html">
            
                
                    <a href="../03day/section.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        ==、is
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="03day/section.1.html">
            
                
                    <a href="../03day/section.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        深拷贝、浅拷贝
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="03day/section.6.html">
            
                
                    <a href="../03day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        进制、位运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="01day/section.3.html">
            
                
                    <a href="../01day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        私有化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="01day/section.3.1.html">
            
                
                    <a href="../01day/section.3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        属性property
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="03day/index.html">
            
                
                    <a href="../03day/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        其他的知识点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="03day/section.0.1.html">
            
                
                    <a href="../03day/section.0.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        垃圾回收(一)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="03day/section.0.1.1.html">
            
                
                    <a href="../03day/section.0.1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        垃圾回收(二)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="03day/section.0.1.2.html">
            
                
                    <a href="../03day/section.0.1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        垃圾回收(三)-gc模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="01day/section.4.html">
            
                
                    <a href="../01day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        内建属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="01day/section.6.html">
            
                
                    <a href="../01day/section.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        内建函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="01day/section.7.html">
            
                
                    <a href="../01day/section.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        集合set
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="02day/section.5.html">
            
                
                    <a href="../02day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        functools
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="03day/section.4.html">
            
                
                    <a href="../03day/section.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        模块
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="03day/section.3.html">
            
                
                    <a href="../03day/section.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        调试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="03day/section.5.html">
            
                
                    <a href="../03day/section.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        编码风格
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >python基础语言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x751F;&#x6210;&#x5668;">&#x751F;&#x6210;&#x5668;</h1>
<h2 id="1-&#x4EC0;&#x4E48;&#x662F;&#x751F;&#x6210;&#x5668;">1. &#x4EC0;&#x4E48;&#x662F;&#x751F;&#x6210;&#x5668;</h2>
<p>&#x901A;&#x8FC7;&#x5217;&#x8868;&#x751F;&#x6210;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x53D7;&#x5230;&#x5185;&#x5B58;&#x9650;&#x5236;&#xFF0C;&#x5217;&#x8868;&#x5BB9;&#x91CF;&#x80AF;&#x5B9A;&#x662F;&#x6709;&#x9650;&#x7684;&#x3002;&#x800C;&#x4E14;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x5305;&#x542B;100&#x4E07;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x5217;&#x8868;&#xFF0C;&#x4E0D;&#x4EC5;&#x5360;&#x7528;&#x5F88;&#x5927;&#x7684;&#x5B58;&#x50A8;&#x7A7A;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4EC5;&#x4EC5;&#x9700;&#x8981;&#x8BBF;&#x95EE;&#x524D;&#x9762;&#x51E0;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x90A3;&#x540E;&#x9762;&#x7EDD;&#x5927;&#x591A;&#x6570;&#x5143;&#x7D20;&#x5360;&#x7528;&#x7684;&#x7A7A;&#x95F4;&#x90FD;&#x767D;&#x767D;&#x6D6A;&#x8D39;&#x4E86;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x5982;&#x679C;&#x5217;&#x8868;&#x5143;&#x7D20;&#x53EF;&#x4EE5;&#x6309;&#x7167;&#x67D0;&#x79CD;&#x7B97;&#x6CD5;&#x63A8;&#x7B97;&#x51FA;&#x6765;&#xFF0C;&#x90A3;&#x6211;&#x4EEC;&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x5728;&#x5FAA;&#x73AF;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#x4E0D;&#x65AD;&#x63A8;&#x7B97;&#x51FA;&#x540E;&#x7EED;&#x7684;&#x5143;&#x7D20;&#x5462;&#xFF1F;&#x8FD9;&#x6837;&#x5C31;&#x4E0D;&#x5FC5;&#x521B;&#x5EFA;&#x5B8C;&#x6574;&#x7684;list&#xFF0C;&#x4ECE;&#x800C;&#x8282;&#x7701;&#x5927;&#x91CF;&#x7684;&#x7A7A;&#x95F4;&#x3002;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x5728;Python&#x4E2D;&#xFF0C;&#x8FD9;&#x79CD;&#x4E00;&#x8FB9;&#x5FAA;&#x73AF;&#x4E00;&#x8FB9;&#x8BA1;&#x7B97;&#x7684;&#x673A;&#x5236;&#xFF0C;&#x79F0;&#x4E3A;&#x751F;&#x6210;&#x5668;&#xFF1A;generator</span>&#x3002;</p>
<h2 id="2-&#x521B;&#x5EFA;&#x751F;&#x6210;&#x5668;&#x65B9;&#x6CD5;1">2. &#x521B;&#x5EFA;&#x751F;&#x6210;&#x5668;&#x65B9;&#x6CD5;1</h2>
<p>&#x8981;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x751F;&#x6210;&#x5668;&#xFF0C;&#x6709;&#x5F88;&#x591A;&#x79CD;&#x65B9;&#x6CD5;&#x3002;&#x7B2C;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x53EA;&#x8981;&#x628A;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#x751F;&#x6210;&#x5F0F;&#x7684; [ ] &#x6539;&#x6210; ( ) </p>
<pre><code class="lang-python">
In [<span class="hljs-number">15</span>]: L = [ x*<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">5</span>)]

In [<span class="hljs-number">16</span>]: L
Out[<span class="hljs-number">16</span>]: [<span class="hljs-number">0</span>, <span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>]

In [<span class="hljs-number">17</span>]: G = ( x*<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">5</span>))

In [<span class="hljs-number">18</span>]: G
Out[<span class="hljs-number">18</span>]: &lt;generator object &lt;genexpr&gt; at <span class="hljs-number">0x7f626c132db0</span>&gt;

In [<span class="hljs-number">19</span>]:
</code></pre>
<p>&#x521B;&#x5EFA; L &#x548C; G &#x7684;&#x533A;&#x522B;&#x4EC5;&#x5728;&#x4E8E;&#x6700;&#x5916;&#x5C42;&#x7684; [ ] &#x548C; ( ) &#xFF0C; L &#x662F;&#x4E00;&#x4E2A;&#x5217;&#x8868;&#xFF0C;&#x800C; G &#x662F;&#x4E00;&#x4E2A;&#x751F;&#x6210;&#x5668;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x6253;&#x5370;&#x51FA;L&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x4F46;&#x6211;&#x4EEC;&#x600E;&#x4E48;&#x6253;&#x5370;&#x51FA;G&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x5462;&#xFF1F;&#x5982;&#x679C;&#x8981;&#x4E00;&#x4E2A;&#x4E00;&#x4E2A;&#x6253;&#x5370;&#x51FA;&#x6765;&#xFF0C;<span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x53EF;&#x4EE5;&#x901A;&#x8FC7; next() &#x51FD;&#x6570;&#x83B7;&#x5F97;&#x751F;&#x6210;&#x5668;&#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x503C;</span>&#xFF1A;</p>
<pre><code class="lang-python">
In [19]: next(G)
Out[19]: 0

In [20]: next(G)
Out[20]: 2

In [21]: next(G)
Out[21]: 4

In [22]: next(G)
Out[22]: 6

In [23]: next(G)
Out[23]: 8

In [24]: next(G)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
&lt;ipython-input-24-380e167d6934&gt; in &lt;module&gt;()
----&gt; 1 next(G)

StopIteration: 

In [25]:
</code></pre>
<pre><code class="lang-python">In [<span class="hljs-number">26</span>]: G = ( x*<span class="hljs-number">2</span> <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">5</span>))

In [<span class="hljs-number">27</span>]: <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> G:
   ....:     print(x)
   ....:     
<span class="hljs-number">0</span>
<span class="hljs-number">2</span>
<span class="hljs-number">4</span>
<span class="hljs-number">6</span>
<span class="hljs-number">8</span>

In [<span class="hljs-number">28</span>]:
</code></pre>
<p>&#x751F;&#x6210;&#x5668;&#x4FDD;&#x5B58;&#x7684;&#x662F;&#x7B97;&#x6CD5;&#xFF0C;&#x6BCF;&#x6B21;&#x8C03;&#x7528; next(G) &#xFF0C;&#x5C31;&#x8BA1;&#x7B97;&#x51FA; G &#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x7684;&#x503C;&#xFF0C;&#x76F4;&#x5230;&#x8BA1;&#x7B97;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x6CA1;&#x6709;&#x66F4;&#x591A;&#x7684;&#x5143;&#x7D20;&#x65F6;&#xFF0C;&#x629B;&#x51FA; StopIteration &#x7684;&#x5F02;&#x5E38;&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x8FD9;&#x79CD;&#x4E0D;&#x65AD;&#x8C03;&#x7528; next() &#x5B9E;&#x5728;&#x662F;&#x592A;&#x53D8;&#x6001;&#x4E86;&#xFF0C;&#x6B63;&#x786E;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x4F7F;&#x7528; for &#x5FAA;&#x73AF;&#xFF0C;&#x56E0;&#x4E3A;&#x751F;&#x6210;&#x5668;&#x4E5F;&#x662F;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x751F;&#x6210;&#x5668;&#x540E;&#xFF0C;&#x57FA;&#x672C;&#x4E0A;&#x6C38;&#x8FDC;&#x4E0D;&#x4F1A;&#x8C03;&#x7528; next() &#xFF0C;&#x800C;&#x662F;&#x901A;&#x8FC7; for &#x5FAA;&#x73AF;&#x6765;&#x8FED;&#x4EE3;&#x5B83;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x9700;&#x8981;&#x5173;&#x5FC3; StopIteration &#x5F02;&#x5E38;&#x3002;</p>
<h2 id="3-&#x521B;&#x5EFA;&#x751F;&#x6210;&#x5668;&#x65B9;&#x6CD5;2">3. &#x521B;&#x5EFA;&#x751F;&#x6210;&#x5668;&#x65B9;&#x6CD5;2</h2>
<p>generator&#x975E;&#x5E38;&#x5F3A;&#x5927;&#x3002;&#x5982;&#x679C;&#x63A8;&#x7B97;&#x7684;&#x7B97;&#x6CD5;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#xFF0C;&#x7528;&#x7C7B;&#x4F3C;&#x5217;&#x8868;&#x751F;&#x6210;&#x5F0F;&#x7684; for &#x5FAA;&#x73AF;&#x65E0;&#x6CD5;&#x5B9E;&#x73B0;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x7528;&#x51FD;&#x6570;&#x6765;&#x5B9E;&#x73B0;&#x3002;</p>
<p>&#x6BD4;&#x5982;&#xFF0C;&#x8457;&#x540D;&#x7684;&#x6590;&#x6CE2;&#x62C9;&#x5951;&#x6570;&#x5217;&#xFF08;Fibonacci&#xFF09;&#xFF0C;&#x9664;&#x7B2C;&#x4E00;&#x4E2A;&#x548C;&#x7B2C;&#x4E8C;&#x4E2A;&#x6570;&#x5916;&#xFF0C;&#x4EFB;&#x610F;&#x4E00;&#x4E2A;&#x6570;&#x90FD;&#x53EF;&#x7531;&#x524D;&#x4E24;&#x4E2A;&#x6570;&#x76F8;&#x52A0;&#x5F97;&#x5230;&#xFF1A;</p>
<p>1, 1, 2, 3, 5, 8, 13, 21, 34, ...</p>
<p>&#x6590;&#x6CE2;&#x62C9;&#x5951;&#x6570;&#x5217;&#x7528;&#x5217;&#x8868;&#x751F;&#x6210;&#x5F0F;&#x5199;&#x4E0D;&#x51FA;&#x6765;&#xFF0C;&#x4F46;&#x662F;&#xFF0C;&#x7528;&#x51FD;&#x6570;&#x628A;&#x5B83;&#x6253;&#x5370;&#x51FA;&#x6765;&#x5374;&#x5F88;&#x5BB9;&#x6613;&#xFF1A;</p>
<pre><code class="lang-python">In [<span class="hljs-number">28</span>]: <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">fib</span><span class="hljs-params">(times)</span>:</span>
   ....:     n = <span class="hljs-number">0</span>
   ....:     a,b = <span class="hljs-number">0</span>,<span class="hljs-number">1</span>
   ....:     <span class="hljs-keyword">while</span> n&lt;times:
   ....:         print(b)
   ....:         a,b = b,a+b
   ....:         n+=<span class="hljs-number">1</span>
   ....:     <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;done&apos;</span>
   ....: 

In [<span class="hljs-number">29</span>]: fib(<span class="hljs-number">5</span>)
<span class="hljs-number">1</span>
<span class="hljs-number">1</span>
<span class="hljs-number">2</span>
<span class="hljs-number">3</span>
<span class="hljs-number">5</span>
Out[<span class="hljs-number">29</span>]: <span class="hljs-string">&apos;done&apos;</span>
</code></pre>
<p>&#x4ED4;&#x7EC6;&#x89C2;&#x5BDF;&#xFF0C;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;fib&#x51FD;&#x6570;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x5B9A;&#x4E49;&#x4E86;&#x6590;&#x6CE2;&#x62C9;&#x5951;&#x6570;&#x5217;&#x7684;&#x63A8;&#x7B97;&#x89C4;&#x5219;&#xFF0C;&#x53EF;&#x4EE5;&#x4ECE;&#x7B2C;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x5F00;&#x59CB;&#xFF0C;&#x63A8;&#x7B97;&#x51FA;&#x540E;&#x7EED;&#x4EFB;&#x610F;&#x7684;&#x5143;&#x7D20;&#xFF0C;&#x8FD9;&#x79CD;&#x903B;&#x8F91;&#x5176;&#x5B9E;&#x975E;&#x5E38;&#x7C7B;&#x4F3C;generator&#x3002;</p>
<p>&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x51FD;&#x6570;&#x548C;generator&#x4EC5;&#x4E00;&#x6B65;&#x4E4B;&#x9065;&#x3002;&#x8981;&#x628A;fib&#x51FD;&#x6570;&#x53D8;&#x6210;generator&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x628A;print(b)&#x6539;&#x4E3A;yield b&#x5C31;&#x53EF;&#x4EE5;&#x4E86;&#xFF1A;</p>
<pre><code class="lang-python">
In [30]: def fib(times):
   ....:     n = 0
   ....:     a,b = 0,1
   ....:     while n&lt;times:
   ....:         yield b
   ....:         a,b = b,a+b
   ....:         n+=1
   ....:     return &apos;done&apos;
   ....: 

In [31]: F = fib(5)

In [32]: next(F)
Out[32]: 1

In [33]: next(F)
Out[33]: 1

In [34]: next(F)
Out[34]: 2

In [35]: next(F)
Out[35]: 3

In [36]: next(F)
Out[36]: 5

In [37]: next(F)
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
&lt;ipython-input-37-8c2b02b4361a&gt; in &lt;module&gt;()
----&gt; 1 next(F)

StopIteration: done
</code></pre>
<p>&#x5728;&#x4E0A;&#x9762;fib &#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6211;&#x4EEC;&#x5728;&#x5FAA;&#x73AF;&#x8FC7;&#x7A0B;&#x4E2D;&#x4E0D;&#x65AD;&#x8C03;&#x7528; yield &#xFF0C;&#x5C31;&#x4F1A;&#x4E0D;&#x65AD;&#x4E2D;&#x65AD;&#x3002;&#x5F53;&#x7136;&#x8981;&#x7ED9;&#x5FAA;&#x73AF;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x6761;&#x4EF6;&#x6765;&#x9000;&#x51FA;&#x5FAA;&#x73AF;&#xFF0C;&#x4E0D;&#x7136;&#x5C31;&#x4F1A;&#x4EA7;&#x751F;&#x4E00;&#x4E2A;&#x65E0;&#x9650;&#x6570;&#x5217;&#x51FA;&#x6765;&#x3002;&#x540C;&#x6837;&#x7684;&#xFF0C;&#x628A;&#x51FD;&#x6570;&#x6539;&#x6210;generator&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x57FA;&#x672C;&#x4E0A;&#x4ECE;&#x6765;&#x4E0D;&#x4F1A;&#x7528; next() &#x6765;&#x83B7;&#x53D6;&#x4E0B;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x800C;&#x662F;&#x76F4;&#x63A5;&#x4F7F;&#x7528; for &#x5FAA;&#x73AF;&#x6765;&#x8FED;&#x4EE3;&#xFF1A;</p>
<pre><code class="lang-python">
In [<span class="hljs-number">38</span>]: <span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> fib(<span class="hljs-number">5</span>):
   ....:     print(n)
   ....:     
<span class="hljs-number">1</span>
<span class="hljs-number">1</span>
<span class="hljs-number">2</span>
<span class="hljs-number">3</span>
<span class="hljs-number">5</span>

In [<span class="hljs-number">39</span>]:
</code></pre>
<p>&#x4F46;&#x662F;&#x7528;for&#x5FAA;&#x73AF;&#x8C03;&#x7528;generator&#x65F6;&#xFF0C;&#x53D1;&#x73B0;&#x62FF;&#x4E0D;&#x5230;generator&#x7684;return&#x8BED;&#x53E5;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;&#x5982;&#x679C;&#x60F3;&#x8981;&#x62FF;&#x5230;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x5FC5;&#x987B;&#x6355;&#x83B7;StopIteration&#x9519;&#x8BEF;&#xFF0C;&#x8FD4;&#x56DE;&#x503C;&#x5305;&#x542B;&#x5728;StopIteration&#x7684;value&#x4E2D;&#xFF1A;</p>
<pre><code class="lang-python">In [<span class="hljs-number">39</span>]: g = fib(<span class="hljs-number">5</span>)

In [<span class="hljs-number">40</span>]: <span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:
   ....:     <span class="hljs-keyword">try</span>:
   ....:         x = next(g)
   ....:         print(<span class="hljs-string">&quot;value:%d&quot;</span>%x)      
   ....:     <span class="hljs-keyword">except</span> StopIteration <span class="hljs-keyword">as</span> e:
   ....:         print(<span class="hljs-string">&quot;&#x751F;&#x6210;&#x5668;&#x8FD4;&#x56DE;&#x503C;:%s&quot;</span>%e.value)
   ....:         <span class="hljs-keyword">break</span>
   ....:     
value:<span class="hljs-number">1</span>
value:<span class="hljs-number">1</span>
value:<span class="hljs-number">2</span>
value:<span class="hljs-number">3</span>
value:<span class="hljs-number">5</span>
&#x751F;&#x6210;&#x5668;&#x8FD4;&#x56DE;&#x503C;:done

In [<span class="hljs-number">41</span>]:
</code></pre>
<h2 id="4-send">4. send</h2>
<p>&#x4F8B;&#x5B50;&#xFF1A;&#x6267;&#x884C;&#x5230;yield&#x65F6;&#xFF0C;gen&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x6682;&#x65F6;&#x4FDD;&#x5B58;&#xFF0C;&#x8FD4;&#x56DE;i&#x7684;&#x503C;;temp&#x63A5;&#x6536;&#x4E0B;&#x6B21;c.send(&quot;python&quot;)&#xFF0C;send&#x53D1;&#x9001;&#x8FC7;&#x6765;&#x7684;&#x503C;&#xFF0C;c.next()&#x7B49;&#x4EF7;c.send(None)</p>
<pre><code class="lang-python">In [<span class="hljs-number">10</span>]: <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">gen</span><span class="hljs-params">()</span>:</span>
   ....:     i = <span class="hljs-number">0</span>
   ....:     <span class="hljs-keyword">while</span> i&lt;<span class="hljs-number">5</span>:
   ....:         temp = <span class="hljs-keyword">yield</span> i
   ....:         print(temp)
   ....:         i+=<span class="hljs-number">1</span>
   ....:
</code></pre>
<h4 id="&#x4F7F;&#x7528;next&#x51FD;&#x6570;">&#x4F7F;&#x7528;next&#x51FD;&#x6570;</h4>
<pre><code class="lang-python">In [11]: f = gen()

In [12]: next(f)
Out[12]: 0

In [13]: next(f)
None
Out[13]: 1

In [14]: next(f)
None
Out[14]: 2

In [15]: next(f)
None
Out[15]: 3

In [16]: next(f)
None
Out[16]: 4

In [17]: next(f)
None
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
&lt;ipython-input-17-468f0afdf1b9&gt; in &lt;module&gt;()
----&gt; 1 next(f)

StopIteration:
</code></pre>
<h4 id="&#x4F7F;&#x7528;next&#x65B9;&#x6CD5;">&#x4F7F;&#x7528;<code>__next__()</code>&#x65B9;&#x6CD5;</h4>
<pre><code class="lang-python">In [18]: f = gen()

In [19]: f.__next__()
Out[19]: 0

In [20]: f.__next__()
None
Out[20]: 1

In [21]: f.__next__()
None
Out[21]: 2

In [22]: f.__next__()
None
Out[22]: 3

In [23]: f.__next__()
None
Out[23]: 4

In [24]: f.__next__()
None
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
&lt;ipython-input-24-39ec527346a9&gt; in &lt;module&gt;()
----&gt; 1 f.__next__()

StopIteration:
</code></pre>
<h4 id="&#x4F7F;&#x7528;send">&#x4F7F;&#x7528;send</h4>
<pre><code class="lang-python">
In [<span class="hljs-number">43</span>]: f = gen()

In [<span class="hljs-number">44</span>]: f.__next__()
Out[<span class="hljs-number">44</span>]: <span class="hljs-number">0</span>

In [<span class="hljs-number">45</span>]: f.send(<span class="hljs-string">&apos;haha&apos;</span>)
haha
Out[<span class="hljs-number">45</span>]: <span class="hljs-number">1</span>

In [<span class="hljs-number">46</span>]: f.__next__()
<span class="hljs-keyword">None</span>
Out[<span class="hljs-number">46</span>]: <span class="hljs-number">2</span>

In [<span class="hljs-number">47</span>]: f.send(<span class="hljs-string">&apos;haha&apos;</span>)
haha
Out[<span class="hljs-number">47</span>]: <span class="hljs-number">3</span>

In [<span class="hljs-number">48</span>]:
</code></pre>
<h2 id="&#x603B;&#x7ED3;"><span class="pg-emphasize pg-emphasize-yellow" style="background: #fff700;">&#x603B;&#x7ED3;</span></h2>
<p>&#x751F;&#x6210;&#x5668;&#x662F;&#x8FD9;&#x6837;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#xFF0C;&#x5B83;&#x8BB0;&#x4F4F;&#x4E0A;&#x4E00;&#x6B21;&#x8FD4;&#x56DE;&#x65F6;&#x5728;&#x51FD;&#x6570;&#x4F53;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#x3002;&#x5BF9;&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;&#x7684;&#x7B2C;&#x4E8C;&#x6B21;&#xFF08;&#x6216;&#x7B2C; n &#x6B21;&#xFF09;&#x8C03;&#x7528;&#x8DF3;&#x8F6C;&#x81F3;&#x8BE5;&#x51FD;&#x6570;&#x4E2D;&#x95F4;&#xFF0C;&#x800C;&#x4E0A;&#x6B21;&#x8C03;&#x7528;&#x7684;&#x6240;&#x6709;&#x5C40;&#x90E8;&#x53D8;&#x91CF;&#x90FD;&#x4FDD;&#x6301;&#x4E0D;&#x53D8;&#x3002;</p>
<p>&#x751F;&#x6210;&#x5668;&#x4E0D;&#x4EC5;&#x201C;&#x8BB0;&#x4F4F;&#x201D;&#x4E86;&#x5B83;&#x6570;&#x636E;&#x72B6;&#x6001;&#xFF1B;&#x751F;&#x6210;&#x5668;&#x8FD8;&#x201C;&#x8BB0;&#x4F4F;&#x201D;&#x4E86;&#x5B83;&#x5728;&#x6D41;&#x63A7;&#x5236;&#x6784;&#x9020;&#xFF08;&#x5728;&#x547D;&#x4EE4;&#x5F0F;&#x7F16;&#x7A0B;&#x4E2D;&#xFF0C;&#x8FD9;&#x79CD;&#x6784;&#x9020;&#x4E0D;&#x53EA;&#x662F;&#x6570;&#x636E;&#x503C;&#xFF09;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#x3002;</p>
<p>&#x751F;&#x6210;&#x5668;&#x7684;&#x7279;&#x70B9;&#xFF1A;</p>
<ol>
<li>&#x8282;&#x7EA6;&#x5185;&#x5B58;</li>
<li>&#x8FED;&#x4EE3;&#x5230;&#x4E0B;&#x4E00;&#x6B21;&#x7684;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x6240;&#x4F7F;&#x7528;&#x7684;&#x53C2;&#x6570;&#x90FD;&#x662F;&#x7B2C;&#x4E00;&#x6B21;&#x6240;&#x4FDD;&#x7559;&#x4E0B;&#x7684;&#xFF0C;&#x5373;&#x662F;&#x8BF4;&#xFF0C;&#x5728;&#x6574;&#x4E2A;&#x6240;&#x6709;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x53C2;&#x6570;&#x90FD;&#x662F;&#x7B2C;&#x4E00;&#x6B21;&#x6240;&#x8C03;&#x7528;&#x65F6;&#x4FDD;&#x7559;&#x7684;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x65B0;&#x521B;&#x5EFA;&#x7684;</li>
</ol>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../01day/section.1.3.html" class="navigation navigation-prev " aria-label="Previous page: __slots__"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../02day/section.3.html" class="navigation navigation-next " aria-label="Next page: 迭代器"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book python基础语言 -->
</html>
